package com.example.httprequestdemos;

import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
public class HttpRequestDemosApplication {
    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        //SpringApplication.run(HttpRequestDemosApplication.class, args);

        HttpRequestGetAndPost hp=new HttpRequestGetAndPost();

        Map<String,Object> param =new HashMap<>();
        param.put("api_name","stock_basic");
        param.put("token","531f7eda35d265a43c7091a5971802aab320a80c16afb6e7164c595a");
        Map<String,String> param2 =new HashMap<>();
        param2.put("list_stauts","L");
        param.put("params",param2);
        param.put("fields","ts_code,name,area,industry,list_date");
        JSONObject json = new JSONObject (param);
        String result =hp.sendPost("http://api.waditu.com",json);
        System.out.println("参数如下：。。。。。。。。。。。。");
        System.out.println(json);
        System.out.println("内容如下：。。。。。。。。。。。。。。。。。。。。。。。");
        //System.out.println(UicodeBackslashU.unicodeToCn(result));
        //System.out.println(result);


        String strpost  = PostDemo.post("http://api.waditu.com",json,null);
        //System.out.println(strpost);

        HttpURLConnectionDemo http = new HttpURLConnectionDemo();

        //System.out.println("Testing 1 - Do Http GET request");
        //http.doGet("http://localhost:8080");
        System.out.println("\nTesting 2 - Do Http POST request");
        System.out.println(http.doPost("http://api.waditu.com"));


    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     *
     * @param unicode
     * @return
     */
    public static String unicodeToCn(String unicode) {
        /** 以 \ u 分割，因为java注释也能识别unicode，因此中间加了一个空格*/
        String[] strs = unicode.split("\\\\u");
        String returnStr = "";
        // 由于unicode字符串以 \ u 开头，因此分割出的第一个字符是""。
        for (int i = 1; i < strs.length; i++) {
            returnStr += (char) Integer.valueOf(strs[i].trim(), 16).intValue();
        }
        return returnStr;
    }

    /**
     *
     * @param cn
     * @return
     */
    public static String cnToUnicode(String cn) {
        char[] chars = cn.toCharArray();
        String returnStr = "";
        for (int i = 0; i < chars.length; i++) {
            returnStr += "\\u" + Integer.toString(chars[i], 16);
        }
        return returnStr;
    }

}
